<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Fx_Page
 *
 * @author anhdt
 */

class Fx_Page extends Fx_Block {

    protected $_url    = '';
    
    protected $_env    = array('Fx_Block', 'Fx_Header', 'Fx_Footer', 'Fx_Resource');

    protected $_header = NULL;

    protected $_body   = NULL;

    protected $_footer = NULL;

    public function __construct($_url = '', $_header = NULL, $_body = NULL, $_footer = NULL, $_isVisible = TRUE) {
        $this->_beforeInit();
        parent::__construct($_url);
        $this->setVisible($_isVisible);
        if (!is_string($_url) || empty($_url)) {
            return $this;
        }
        $this->_url = $_url;
        if (!empty($_header)) {
            $this->setHeader($_header);
        }
        if (!empty($_body)) {
            $this->setBody($_body);
        }
        if (!empty($_footer)) {
            $this->setFooter($_footer);
        }
    }

    public function getUrl() {
        return $this->_url;
    }

    public function setUrl($_url) {
        if (is_string($_url)) {
            $this->_url = $_url;
        }
        return $this;
    }

    /**
     * GET SINGLETON OBJEC
     * @return Fx_Page
     */
    public static function getInstance() {
        if (NULL === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * Set Header Of Page
     * @param Fx_Header (Header Of Page)
     * @return Fx_Page
     */
    public function setHeader(Fx_Header $header = null) {
        if (!empty($header)) {
            $this->_header = $header;
        }
        return $this;
    }

    /**
     * Get Header Of Page
     * @return Fx_Header
     */
    public function getHeader() {
        return $this->_header;
    }

    /**
     * Set Body Of Page
     * @param Fx_Block (Body Of Page)
     * @return Fx_Page
     */
    public function setBody($body = NULL) {
        if (empty($body) || !$body instanceof Fx_Block) {
            return $this;
        }
        $this->_body = $body;
        return $this;
    }

    /**
     * Get Body Of Page
     * @return Fx_Block
     */
    public function getBody() {
        return $this->_body;
    }

    /**
     * Set Footer Of Page
     * @param Fx_Footer (Footer Of Page)
     * @return Fx_Page
     */
    public function setFooter(Fx_Header $footer = null) {
        if (!empty($footer)) {
            $this->_footer = $footer;
        }
        return $this;
    }

    /**
     * Get Footer Of Page
     * @return Fx_Footer
     */
    public function getFooter() {
        return $this->_footer;
    }

    public function toHtml() {
        echo $this->getHeader()->getOutputHtml();
        echo $this->getBody()->getOutputHtml();
        echo $this->getFooter()->getOutputHtml();
    }

    public function show() {
        echo $this->getOutputHtml();
        return $this;
    }

    public function countChildren() {
        return count($this->_children);
    }
}

